using Microsoft.EntityFrameworkCore;
using STD.Services.UserService.Domain.Entities;
using STD.Services.UserService.Domain.Repositories;
using STD.Shared.Domain.ValueObjects;

namespace STD.Services.UserService.Infrastructure.Data;

/// <summary>
/// 用户仓储实现
/// </summary>
public class UserRepository : IUserRepository
{
    private readonly UserDbContext _context;
    
    public UserRepository(UserDbContext context)
    {
        _context = context;
    }
    
    /// <summary>
    /// 根据ID获取用户
    /// </summary>
    /// <param name="id">用户ID</param>
    /// <returns>用户</returns>
    public async Task<User?> GetByIdAsync(Guid id)
    {
        return await _context.Users
            .FirstOrDefaultAsync(u => u.Id == id && !u.IsDeleted);
    }
    
    /// <summary>
    /// 根据用户名获取用户
    /// </summary>
    /// <param name="username">用户名</param>
    /// <returns>用户</returns>
    public async Task<User?> GetByUsernameAsync(string username)
    {
        return await _context.Users
            .FirstOrDefaultAsync(u => u.Username == username && !u.IsDeleted);
    }
    
    /// <summary>
    /// 根据邮箱获取用户
    /// </summary>
    /// <param name="email">邮箱</param>
    /// <returns>用户</returns>
    public async Task<User?> GetByEmailAsync(string email)
    {
        return await _context.Users
            .FirstOrDefaultAsync(u => u.Email.Value == email && !u.IsDeleted);
    }
    
    /// <summary>
    /// 获取所有用户
    /// </summary>
    /// <param name="skip">跳过数量</param>
    /// <param name="take">获取数量</param>
    /// <returns>用户列表</returns>
    public async Task<IEnumerable<User>> GetAllAsync(int skip = 0, int take = 100)
    {
        return await _context.Users
            .Where(u => !u.IsDeleted)
            .OrderBy(u => u.CreatedAt)
            .Skip(skip)
            .Take(take)
            .ToListAsync();
    }
    
    /// <summary>
    /// 添加用户
    /// </summary>
    /// <param name="user">用户</param>
    /// <returns>任务</returns>
    public async Task AddAsync(User user)
    {
        await _context.Users.AddAsync(user);
        await _context.SaveChangesAsync();
    }
    
    /// <summary>
    /// 更新用户
    /// </summary>
    /// <param name="user">用户</param>
    /// <returns>任务</returns>
    public async Task UpdateAsync(User user)
    {
        _context.Users.Update(user);
        await _context.SaveChangesAsync();
    }
    
    /// <summary>
    /// 删除用户
    /// </summary>
    /// <param name="user">用户</param>
    /// <returns>任务</returns>
    public async Task DeleteAsync(User user)
    {
        user.Delete();
        await _context.SaveChangesAsync();
    }
    
    /// <summary>
    /// 检查用户名是否存在
    /// </summary>
    /// <param name="username">用户名</param>
    /// <returns>是否存在</returns>
    public async Task<bool> ExistsByUsernameAsync(string username)
    {
        return await _context.Users
            .AnyAsync(u => u.Username == username && !u.IsDeleted);
    }
    
    /// <summary>
    /// 检查邮箱是否存在
    /// </summary>
    /// <param name="email">邮箱</param>
    /// <returns>是否存在</returns>
    public async Task<bool> ExistsByEmailAsync(string email)
    {
        return await _context.Users
            .AnyAsync(u => u.Email.Value == email && !u.IsDeleted);
    }
} 